package com.itheima.service.impl;

import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.databind.ser.std.UUIDSerializer;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.SysDictDataMapper;
import com.itheima.dao.SysDictTypeMapper;
import com.itheima.pojo.SysDictType;
import com.itheima.service.SysDictTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import javax.persistence.Id;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author xuchangshuo
 * @date 2020/8/3 20:42
 */
@Service
public class SysDictTypeServiceImpl implements SysDictTypeService {

    @Autowired
    private SysDictTypeMapper sysDictTypeMapper;
    @Autowired
    private SysDictDataMapper sysDictDataMapper;

    @Override
    public void add(Map<String, Object> map) {
//        //删除字段，将map封装成sysDictType对象
//        map.remove("keyword");
//        map.remove("pageSize");
//        map.remove("currentPage");
//        //将map转为json字符串
//        String jsonString = JSON.toJSONString(map);
        //将json串转为sysDictType对象
        String jsonString = remove(map);
        SysDictType sysDictType = JSON.parseObject(jsonString, SysDictType.class);
        sysDictTypeMapper.insertSelective(sysDictType);
    }

    @Override
    public void edit(Map<String, Object> map) {
        String jsonString = remove(map);
        SysDictType sysDictType = JSON.parseObject(jsonString, SysDictType.class);
        sysDictTypeMapper.updateByPrimaryKeySelective(sysDictType);

    }

    @Override
    public Page<SysDictType> getList(Map<String, Object> map) {
        Integer pageSize = (Integer) map.get("pageSize");
        if (pageSize == null) {
            pageSize = 10;
        }
        Integer currentPage = (Integer) map.get("currentPage");
        PageHelper.startPage(currentPage, pageSize);
        Example example = new Example(SysDictType.class);
        Example.Criteria criteria = example.createCriteria();
        if (map!=null){
            if (map.get("dictType")!=null||!"".equals(map.get("dictType"))){
                criteria.andEqualTo("dictType", map.get("dictType"));
            }
            if (map.get("dictName")!=null||!"".equals(map.get("dictName"))){
                criteria.andEqualTo("dictName", map.get("dictName"));
            }
        }

        Page<SysDictType> sysDictTypeList = (Page<SysDictType>) sysDictTypeMapper.selectByExample(example);
        return sysDictTypeList;
    }

    @Override
    public void deleteBatch(List<Map<String, Object>> list) {
        //创建一个集合 接受map中的uid
        ArrayList<String> uids = new ArrayList<>();
        for (Map<String, Object> map : list) {
            String uid = (String) map.get("uid");
            uids.add(uid);
        }
        for (String uid : uids) {
            //判断该字典类型是否有关联的字典数据
            int count = sysDictDataMapper.getSysDictDataCount(uid);
            if (count == 0) {  //如果没有则可以删除该字典类型
                sysDictTypeMapper.deleteByPrimaryKey(uid);
            } else {  //如果有关联字典数据，则抛出异常不能删除
                throw new RuntimeException("该类型不允许删除");
            }
        }
    }

    //对传入的map集合进行处理
    public String remove(Map<String, Object> map) {
        map.remove("keyword");
        map.remove("pageSize");
        map.remove("currentPage");
        //将map转为json字符串
        String jsonString = JSON.toJSONString(map);

        return jsonString;
    }
}
